package com.qf.controller;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FastFileStorageClient client;

    @Value("${fdfs.nginxHome}")
    private String nginxHome;

    @PostMapping("/upload")
    public String upload(MultipartFile file) throws IOException {
        InputStream ips = file.getInputStream();
        long size = file.getSize();

        // 获取文件的后缀
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        StorePath storePath = client.uploadImageAndCrtThumbImage(ips, size, extension, null);

        String fielUrl = storePath.getFullPath();
        System.out.println(nginxHome+fielUrl);
        return nginxHome+fielUrl;
    }
}
